const {level} = require('province-city-china/data')
const database = require("../database/sqliteCRUD");


class SnippetService {
    cityStr = []
    level

    constructor() {
    }

    /**
     * 获取到中间转折词
     * @return {string}
     */
    getMiddleWords() {
        let conjunctions = [
            "虽然", "但是", "可是", "然而", "如果", "要是", "假如", "要是",
            "并且", "而且", "也", "还", "不但", "不仅", "而且", "更",
            "因为", "所以", "于是", "因此", "只要", "除非", "只有", "除非",
            "首先", "然后", "接着", "最后", "即使", "尽管", "纵使", "哪怕",
            "为了", "以便", "以免", "以免", "另外", "此外", "另外还有", "再"
        ];
        let len = conjunctions.length

        return conjunctions[this.randomNumber(0, len)]
    }

    /**
     * @desc 该函数随机输出一个中国地名 若存在会精确到县级（区级）
     *
     * data - 总数据(省/地/县/乡)
     *
     * province - 省级(省/直辖市/特别行政区)
     *
     * city - 地级(城市)
     *
     * area - 县级(区县)
     *
     * town - 乡级(乡镇/街)
     *
     * level -  tree
     *
     * @return String
     */
    getCity() {
        let next
        let isGo = true
        if (this.cityStr.length > 0) this.cityStr = []
        let deep = (object) => {

            try {
                if (Object.prototype.toString.call(object) === '[object Array]') {

                    next = object[this.randomNumber(0, object.length - 1)]

                    this.cityStr.push(next.name)
                }
                if (Object.keys(next).includes('children')) {
                    deep(next.children)
                }
            } catch (e) {
                deep(level)
            }
            return this.cityStr.join('')
        }
        return deep(level)
    }

    async getSentenceByRand() {
        // 取出id数组 随机选择一个id  使where 等于 id = ${ id }

        let idArr = await database.customSqlExec(`select id
                                                  from sentence`)
        // 取到随机id
        let id = idArr[this.randomNumber(0, idArr.length)].id
        // 根据id取到当前数据
        return database.autoCRUD.getRows('sentence', `id = ${id}`, '0,10')

    }
}

SnippetService.prototype.randomNumber = (min, max) => Math.floor(Math.random() * (max - min) + min);

module.exports = snippetService = new SnippetService()